﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AllenalaMente.Games.Lost
{
    public partial class Item : UserControl
    {
        public Point position;
        public string path;

        public Item(string ImagePath, Point position)
        {
            InitializeComponent();

            this.position = position;
            this.path = ImagePath;

            Canvas.SetLeft(imageItem, position.X);
            Canvas.SetTop(imageItem, position.Y);

            Canvas.SetLeft(rectangle, position.X);
            Canvas.SetTop(rectangle, position.Y);

            // Carica Immagine
            Uri uri = new Uri(ImagePath, UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
            imageItem.SetValue(Image.SourceProperty, img);
        }

        public void setDimension(int w, int h)
        {
            LayoutRoot.Width = w;
            LayoutRoot.Height = h;

            imageItem.Width = w;
            imageItem.Height = h;

            rectangle.Width = w;
            rectangle.Height = h;
        }

        #region Animations
        
        private  Storyboard sb;

        private Point HidePosition = new Point(365, 50);
        private Point HidePosition2 = new Point(365, 200);

        // HIDE

        public void Hide()
        {
            Move(0, 0, (int)(HidePosition.X - position.X), (int)(HidePosition.Y - position.Y));
            sb.Completed += sb_Hide_Completed;
        }

        void sb_Hide_Completed(object sender, EventArgs e)
        {
            sb.Completed -= sb_Hide_Completed;
 
            Move((int)(HidePosition.X - position.X), (int)(HidePosition.Y - position.Y), (int)(HidePosition2.X - position.X), (int)(HidePosition2.Y - position.Y));
        }

        // SHOW

        public void Show()
        {
            Move((int)(HidePosition2.X - position.X), (int)(HidePosition2.Y - position.Y), (int)(HidePosition.X - position.X), (int)(HidePosition.Y - position.Y));
            sb.Completed += sb_Show_Completed;
        }

        void sb_Show_Completed(object sender, EventArgs e)
        {
            sb.Completed -= sb_Show_Completed;

            Move((int)(HidePosition.X - position.X), (int)(HidePosition.Y - position.Y), 0 , 0);
        }

        // MOVE

        private bool isMoving = false;

        public void Move(int FromX, int FromY, int ToX, int ToY)
        {
            if (isMoving) return;

            isMoving = true;

            DoubleAnimation doubleAnimationY = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromSeconds(0.5)),
                EasingFunction = new BackEase() { EasingMode = EasingMode.EaseInOut }
            };

            DoubleAnimation doubleAnimationX = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromSeconds(0.5)),
                EasingFunction = new BackEase() { EasingMode = EasingMode.EaseInOut }
            };

            sb = new Storyboard();

            imageItem.RenderTransform = new TranslateTransform();

            doubleAnimationY.From = FromY;
            doubleAnimationY.To = ToY;

            doubleAnimationX.From = FromX;
            doubleAnimationX.To = ToX;

            Storyboard.SetTargetProperty(doubleAnimationY, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
            Storyboard.SetTargetProperty(doubleAnimationX, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));

            Storyboard.SetTarget(doubleAnimationY, imageItem);
            Storyboard.SetTarget(doubleAnimationX, imageItem);

            sb.Children.Add(doubleAnimationY);
            sb.Children.Add(doubleAnimationX);

            sb.Completed += new EventHandler(sb_MoveCompleted);
            sb.Begin();
        }

        void sb_MoveCompleted(object sender, EventArgs e)
        {
            isMoving = false;
        }

        // FADE IN
        public void FadeIn()
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 0;
            doubleAnimation.To = 1;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, this);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            sb.Completed += new EventHandler(sb_ShowCompleted);
            sb.Begin();
        }

        void sb_ShowCompleted(object sender, EventArgs e)
        {
            this.Opacity = 1;
        }

        #endregion
    }
}
